package com.typhoon.spring_shiro.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.typhoon.spring_shiro.entity.Stock;
import com.typhoon.spring_shiro.utils.CommonUtils;

/**
 * StockDao实现
 * 
 * @author Typhoon
 * @date 2017-07-11 12:46
 * @since V1.3.0
 */
@Repository("stockDao")
public class StockDao extends BaseDao implements ICommonDao<Stock, Long> {

	private static final Logger LOGGER = LoggerFactory.getLogger(StockDao.class);

	private final static String TABLE_STOCK = "Stock";

	@Resource
	private JdbcTemplate centerJdbc;

	@Override
	public Stock queryByPrimaryKey(Long primaryKey) {
		return this.queryByPrimaryKey(centerJdbc, TABLE_STOCK, primaryKey, Stock.class);
	}

	@Override
	public int deleteByPrimaryKey(Long primaryKey) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Stock get(Object paramObject) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Stock> list(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Stock> list(Object paramObject) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Stock paramE) {
		return this.updateByPrimaryKey(centerJdbc, paramE, TABLE_STOCK, "id");
	}

	@Override
	public long count(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long count(Object paramObject) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Stock save(Stock paramE) {
		return this.addObjForKey(centerJdbc, TABLE_STOCK, paramE, "id");
	}

	/**
	 * 调整库存(有可能增加也有可能减少)
	 * 
	 * @author Typhoon
	 * @date 2017-07-11 16:10
	 * @since V1.3.0
	 * @description <p>由增加和扣减非幂等操作转换成幂等操作</p>
	 * @param params
	 * @return
	 */
	public int adjustStock(Map<String, Object> params) {
		if (null == params || params.isEmpty()) {
			return -1;
		}
		StringBuilder sql = new StringBuilder("	update	");
		sql.append(TABLE_STOCK);
		sql.append("	set stock = ").append(CommonUtils.evalInt(params.get("newStock")));
		sql.append(",version = version + 1	");
		sql.append(" ,updateTime =  ").append("'").append(params.get("updateTime")).append("'");
		sql.append(where).append(oneEqOne);
		sql.append(and).append("	id = 	").append(CommonUtils.evalLong(params.get("id")));
		sql.append(and).append("	stock = 	").append(CommonUtils.evalInt(params.get("oldStock")));
		sql.append(and).append("	version = 	").append(CommonUtils.evalInt(params.get("version")));
		LOGGER.info("updatesql:{}", sql.toString());
		return this.executeSQL(centerJdbc, sql.toString());
	}

	/**
	 * 扣减库存操作
	 * 
	 * @author Typhoon
	 * @date 2017-07-12 17:34 Wednesday
	 * @since V1.3.0
	 * @param params
	 * @return
	 */
	public int deductStock(Map<String, Object> params) {
		if (null == params || params.isEmpty()) {
			return -1;
		}
		StringBuilder sql = new StringBuilder("	update	");
		sql.append(TABLE_STOCK);
		sql.append("	set stock = stock - 	").append(params.get("needDeduct"));
		sql.append(" ,updateTime =  ").append("'").append(params.get("updateTime")).append("'");
		sql.append(where).append("	id = 	").append(CommonUtils.evalLong(params.get("id")));
		sql.append(and).append("	stock > 	").append(params.get("needDeduct"));
		LOGGER.info("updatesql:{}", sql.toString());
		return this.executeSQL(centerJdbc, sql.toString());
	}

}
